package cn.xshi.bpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.bpm.service.*;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.constant.PathConstant;
import cn.xshi.common.idgeneration.SnowflakeIdWorker;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.common.util.file.FileUtil;
import cn.xshi.file.dao.AttachmentDao;
import cn.xshi.file.model.Attachment;
import cn.xshi.bpm.design.MxGraphModel;
import cn.xshi.bpm.design.MxGraphToBPMN;
import cn.xshi.bpm.design.MxGraphToPng;
import cn.xshi.bpm.model.LcNodeAttribute;
import cn.xshi.bpm.model.LcProduct;
import cn.xshi.bpm.param.LcProcessParam;
import cn.xshi.bpm.util.ActivitiUtil;
import cn.xshi.bpm.dao.LcDeploymentHisDao;
import cn.xshi.bpm.dao.LcProcessDao;
import cn.xshi.bpm.model.LcDeploymentHis;
import cn.xshi.bpm.model.LcProcess;
import cn.xshi.bpm.util.Constant;
import cn.xshi.bpm.vo.LcProcessEntity;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.repository.Deployment;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 流程信息存储记录
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
@Slf4j
public class LcProcessServiceImpl extends BaseService implements LcProcessService {

	@Resource
	LcProcessDao lcProcessDao;

	@Resource
	LcDeploymentHisDao lcDeploymentHisDao;

	@Resource
	ActivitiUtil activitiUtil;

	@Resource
	AttachmentDao attachmentDao;

	@Resource
    LcDeploymentHisService lcDeploymentHisService;

	@Resource
    LcNodeAttributeService lcNodeAttributeService;

	@Resource
    LcTaskService lcTaskService;

	@Resource
	SnowflakeIdWorker snowflakeIdWorker;

	@Resource
    LcProductService lcProductService;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<LcProcess> getLcProcessListByCondition(Map<String,Object> condition){
		try{
			return lcProcessDao.getLcProcessListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	/**
	* 查询对象
	* @param lc_process_id 
	* @return
	*/
	public LcProcess getLcProcessById(String lc_process_id){
		try{
			return lcProcessDao.getLcProcessById(lc_process_id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	/**
	* 添加
	* @param lcProcess
	* @return
	*/
	public int addLcProcess(LcProcess lcProcess){
		int i = 0;
		try {
            lcProcess.setCreateTime(getDate());
			lcProcess.setCreateId(getCurrentAccountId());
			i = lcProcessDao.addLcProcess(lcProcess);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param lcProcess
	* @return
	*/
	public int updateLcProcess(LcProcess lcProcess){
		int i = 0;
		try {
			lcProcess.setUpdateTime(getDate());
			lcProcess.setUpdateId(getCurrentAccountId());
			i = lcProcessDao.updateLcProcess(lcProcess);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delLcProcess(Map<String,Object> condition){
		int i = 0;
		try {
			i = lcProcessDao.delLcProcess(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 发布或关闭流程
	 * @param condition
	 * @return
	 */
	public int updateLcProcessStatus(String id,Map<String,Object> condition){
		int i = 0;
		try {
			String attachPath;
			String tenantId = "";
			if(StringUtil.isEmpty(id)){
				throw new ExceptionUtil("流程编号为空");
			}
			LcProcess lcProcess = lcProcessDao.getLcProcessById(id);
			if(null== lcProcess){
				throw new ExceptionUtil("该流程为空，无法发布");
			}
			if(1==lcProcess.getStatus()){
				throw new ExceptionUtil("该流程已启动中，无法发布");
			}
			if(!StringUtil.isEmpty(lcProcess.getLcProductId())){
				LcProduct lcProduct = lcProductService.getLcProductById(lcProcess.getLcProductId());
				if(null != lcProduct){
					tenantId = lcProduct.getTenantId();
				}
			}
			String versionPath = "";
			if(null != lcProcess.getVersion()){
				versionPath = lcProcess.getVersion()+"/";
			}
			Deployment deployment = null;
			if(1==lcProcess.getFlag()){//如果上传部署则
				Attachment attachment = attachmentDao.getAttachmentById(lcProcess.getAttachmentId());
				attachPath = getSysPathCache(Constant.ACTIVITI_LC).get(0).getPath()+"/"+ attachment.getName();
				File file = new File(attachPath);
				if(!file.exists()){//再次从默认目录拿
					attachPath =getSysPathCache(PathConstant.SYS_SOURCES_DEFAULT_PATH).get(0).getPath()+"/"+ attachment.getName();
					file = new File(attachPath);
				}
				if(!file.exists()){
					throw new ExceptionUtil("流程文件不存在，部署失败");
				}
				if(!StringUtil.isEmpty(tenantId)){
					deployment = activitiUtil.createDeployment(file,tenantId);
				}else{
					deployment = activitiUtil.createDeployment(file);
				}
			}else{
				if(!StringUtil.isEmpty(tenantId)){
					deployment = activitiUtil.createDeployment(lcProcess.getTitle(),lcProcess.getBpmn(),tenantId);//通过bpmn字符串部署
				}else{
					deployment = activitiUtil.createDeployment(lcProcess.getTitle(),lcProcess.getBpmn());//通过bpmn字符串部署
				}
			}
			if(null != deployment){
				LcDeploymentHis lcDeploymentHis = new LcDeploymentHis();
				lcDeploymentHis.setId(toUUID());
				lcDeploymentHis.setDeploymentId(deployment.getId());
				lcDeploymentHis.setDeploymentName(lcProcess.getTitle()+"_"+deployment.getId());
				lcDeploymentHis.setTenantId(deployment.getTenantId());
				lcDeploymentHis.setDeploymentTime(getDate());
				lcDeploymentHis.setLcProcessId(id);
				lcDeploymentHis.setStatus(0);
				lcDeploymentHis.setVersion(lcProcess.getVersion());
				lcDeploymentHis.setUid(lcProcess.getUid());
				lcDeploymentHis.setUk(lcProcess.getUk());
				lcDeploymentHis.setBpmn(lcProcess.getBpmn());
				lcDeploymentHis.setMxgraphStyle(lcProcess.getMxgraphStyle());
				lcDeploymentHis.setMxgraphXml(lcProcess.getMxgraphXml());
				lcDeploymentHis.setImgXml(lcProcess.getImgXml());
				lcDeploymentHis.setH(lcProcess.getH());
				lcDeploymentHis.setW(lcProcess.getW());
				lcDeploymentHis.setCreateId(getCurrentAccountId());
				lcDeploymentHis.setCreateTime(getDate());
				lcDeploymentHis.setCandidateGroupType(lcProcess.getCandidateGroupType());
				lcDeploymentHis.setCandidateStarterGroups(lcProcess.getCandidateStarterGroups());
				lcDeploymentHis.setCandidateStarterUsers(lcProcess.getCandidateStarterUsers());
				lcDeploymentHisDao.addLcDeploymentHis(lcDeploymentHis);
				syncAttr(lcDeploymentHis);//同步节点至版本中
			}else{
				throw new ExceptionUtil("返回流程部署信息为空");
			}
			i = lcProcessDao.updateLcProcessStatus(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 根据流程模块Key查找流程定义
	 * @param moduleKey
	 * @return
	 */
	public LcProcess getLcProcessByModuleKey(String moduleKey){
		return lcProcessDao.getLcProcessByModuleKey(moduleKey);
	}

	/**
	 * 在线设计
	 * @param mxGraphModel
	 * @param request
	 * @param response
	 * @return
	 */
	public synchronized BaseResult<LcProcess> create(MxGraphModel mxGraphModel, HttpServletRequest request, HttpServletResponse response){
		if(StringUtil.isEmpty(mxGraphModel.getTitle())){
			return new BaseResult("流程标题为空",false);
		}
		BaseResult baseResult = new BaseResult();
		String mxgraphXml = mxGraphModel.getMxgraphXml();
		String imgXml = mxGraphModel.getImgXml();
		mxGraphModel.setMxgraphXml(mxgraphXml);
		mxGraphModel.setImgXml(imgXml);
		MxGraphToBPMN mxGraphToBPMN = new MxGraphToBPMN();
		mxGraphModel = mxGraphToBPMN.createBPMN(mxGraphModel);
		LcProcess lcProcess = new LcProcess();
		//////////添加或修改流程信息///////////////
		lcProcess.setFlag(0);
		lcProcess.setBpmn(mxGraphModel.getBpmn());
		lcProcess.setUid(mxGraphModel.getUid());
		lcProcess.setUk(mxGraphModel.getUk());
		lcProcess.setRemark(mxGraphModel.getRemark());
		lcProcess.setMxgraphXml(mxGraphModel.getMxgraphXml());
		lcProcess.setStatus(0);
		lcProcess.setTitle(mxGraphModel.getTitle());
		lcProcess.setImgXml(mxGraphModel.getImgXml());
		lcProcess.setId(mxGraphModel.getId());
		lcProcess.setLcProductId(mxGraphModel.getLcProductId());
		lcProcess.setLcGroupId(mxGraphModel.getLcGroupId());
		lcProcess = generateBpmnAndImg(request.getRequestURL().toString(), imgXml, mxGraphModel.getBpmn(), mxGraphModel.getTitle(), mxGraphModel.getW(), mxGraphModel.getH(), response, lcProcess);
		lcProcess.setBpmnPath(lcProcess.getBpmnPath());
		lcProcess.setPath(lcProcess.getPath());
		lcProcess.setImgPath(lcProcess.getImgPath());
		lcProcess.setH(mxGraphModel.getH());
		lcProcess.setW(mxGraphModel.getW());
		lcProcess.setId(mxGraphModel.getId());
		lcProcess.setModuleKey(StringUtil.isEmpty(mxGraphModel.getModuleKey())?genModuleKey():mxGraphModel.getModuleKey());
		lcProcess.setCandidateStarterGroups(mxGraphModel.getCandidateStarterGroups());
		lcProcess.setCandidateStarterUsers(mxGraphModel.getCandidateStarterUsers());
		lcProcess.setCandidateGroupType(mxGraphModel.getCandidateGroupType());
		int i = 0;
		if(StringUtils.isEmpty(lcProcess.getId())){
			lcProcess.setId(toUUID());
			i = addLcProcess(lcProcess);
		}else{
			if(mxGraphModel.isFromImport()){//当流程id存在并且来源是导入 则判断是否已存在 如果存在 则更新 否则新增
				LcProcess process = getLcProcessById(lcProcess.getId());
				if(null != process){
					i = updateLcProcess(lcProcess);
				}else{
					i = addLcProcess(lcProcess);
				}
			}else{
				i = updateLcProcess(lcProcess);
			}
		}
		if(!mxGraphModel.isFromImport()){//当流程id存在并且来源是导入 则判断是否已存在 如果存在 则更新 否则新增
			doAttr(lcProcess);//处理节点规则
		}
		baseResult.setData(lcProcess);
		if(i > 0){
			baseResult.setSuccess(true);
			baseResult.setMessage("保存流程成功");
			return baseResult;
		}
		baseResult.setSuccess(false);
		baseResult.setMessage("保存流程失败");
		return baseResult;
	}

	/**
	 * 生成bpmn,图片两个文件及压缩文件
	 * @param url
	 * @param imgxml
	 * @param bpmn
	 * @param processName
	 * @param w
	 * @param h
	 * @param response
	 * @param lcProcess
	 * @return
	 */
	public LcProcess generateBpmnAndImg(String url, String imgxml, String bpmn, String processName, String w, String h, HttpServletResponse response, LcProcess lcProcess) {
		int version = reBuild(lcProcess);
		lcProcess.setVersion(version);
		String versionPath = "";
		if(null != lcProcess.getVersion()){
			versionPath = lcProcess.getVersion()+"/";
		}
		String attachPath = FileUtil.validOrCreateFile(getSysPathCache(Constant.ACTIVITI_LC).get(0).getPath()+processName+"/"+versionPath);
		//生成BPMN文件
		String bpmnAttachPath = attachPath+processName+".bpmn";
		String imgAttachPath =  attachPath+processName+".png";
		try {
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(bpmnAttachPath),"UTF8");
			out.write(bpmn);
			out.flush();
			out.close();
		} catch (IOException e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		//通过mxgraphxml字符串生成PNG图片
		MxGraphToPng MxGraphToPng = new MxGraphToPng();
		int j = MxGraphToPng.mxgraphxmlToPng(url,imgxml,imgAttachPath,w,h,response);
		if(j <= 0){
			throw new ExceptionUtil("由bpmn文件生成图片失败");
		}
		//生成ZIP文件
		try{
			File jpdlAttachPathFile = new File(bpmnAttachPath);
			File imgAttachPathFile = new File(imgAttachPath);
			File zipFile = new File(attachPath+processName+".zip");
			File[] srcfile={jpdlAttachPathFile,imgAttachPathFile};
			FileUtil.zipFiles(srcfile,zipFile);
			lcProcess.setBpmnPath(processName+".bpmn");
			lcProcess.setPath(processName+".zip");
			lcProcess.setImgPath(processName+".png");
		}catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return lcProcess;
	}

	/**
	 * 设置版本
	 * @param lcProcess
	 */
	private synchronized int reBuild(LcProcess lcProcess){
		if(!StringUtil.isEmpty(lcProcess.getId())){
			Integer maxVersion = lcDeploymentHisDao.getMaxVersionByProcessId(lcProcess.getId());
			if(null != maxVersion) {//如果发布中流程进行重新设计 则生成文件需要重新生成并加上版本号
				return (maxVersion.intValue() + 1);
			}
		}
		return 0;
	}

	/**
	 * 复制
	 * @param lcProcessParam
	 * @return
	 */
	public BaseResult copy(LcProcessParam lcProcessParam, HttpServletRequest request, HttpServletResponse response){
		if(StringUtil.isEmpty(lcProcessParam.getId())){
			return new BaseResult("未能获取到流程id",false);
		}
		if(StringUtil.isEmpty(lcProcessParam.getTitle())){
			return new BaseResult("流程标题为空",false);
		}
		if(StringUtil.isEmpty(lcProcessParam.getModuleKey())){
			return new BaseResult("流程模块Key为空",false);
		}
		LcProcess lcProcess = getLcProcessById(lcProcessParam.getId());
		if(null == lcProcess){
			return new BaseResult("未能获取到流程对象",false);
		}
		Map<String,Object> condition = new HashMap<>();
		condition.put("moduleKey",lcProcessParam.getModuleKey());
		List<LcProcess> list = lcProcessDao.getLcProcessListByCondition(condition);
		if(!CollectionUtil.isEmpty(list)){
			return new BaseResult("流程模块Key已存在",false);
		}
		MxGraphModel mxGraphModel = new MxGraphModel();
		mxGraphModel.setBpmn(null);
		mxGraphModel.setCandidateGroupType(lcProcess.getCandidateGroupType());
		mxGraphModel.setImgXml(lcProcess.getImgXml());
		mxGraphModel.setMxgraphXml(lcProcess.getMxgraphXml());
		mxGraphModel.setW(lcProcess.getW());
		mxGraphModel.setH(lcProcess.getH());
		mxGraphModel.setId(null);
		mxGraphModel.setCandidateStarterGroups(lcProcess.getCandidateStarterGroups());
		mxGraphModel.setCandidateStarterUsers(lcProcess.getCandidateStarterUsers());
		mxGraphModel.setRemark(lcProcess.getRemark());
		mxGraphModel.setModuleKey(lcProcessParam.getModuleKey());
		mxGraphModel.setLcProductId(lcProcessParam.getLcProductId());
		mxGraphModel.setLcGroupId(lcProcessParam.getLcGroupId());
		mxGraphModel.setTitle(lcProcessParam.getTitle());
		BaseResult<LcProcess> baseResult = create(mxGraphModel,request,response);
		if(!baseResult.getSuccess()){
			return BaseResult.fail("复制主数据异常");
		}
		if(0 == lcProcessParam.getSyncAttr()){
			//同步扩展属性
			condition = new HashMap<>();
			condition.put("mountsId",lcProcessParam.getId());
			List<LcNodeAttribute> lcNodeAttributes = lcNodeAttributeService.getLcNodeAttributeListByCondition(condition);
			if(!CollectionUtil.isEmpty(lcNodeAttributes)){
				for(LcNodeAttribute lcNodeAttribute: lcNodeAttributes){
					lcNodeAttribute = lcNodeAttributeService.getLcNodeAttributeById(lcNodeAttribute.getId());
					lcNodeAttribute.setMountsId(baseResult.getData().getId());
					lcNodeAttribute.setId(null);
					lcNodeAttribute.setHid_(null);
					lcNodeAttribute.setLcProcessId(baseResult.getData().getId());
					lcNodeAttributeService.saveLcNodeAttribute(lcNodeAttribute);
				}
			}
		}
		return BaseResult.success();
	}

	/**
	 * 同步主节点配置信息至子表中
	 * @param lcDeploymentHis
	 */
	private void syncAttr(LcDeploymentHis lcDeploymentHis){
		String hid = lcDeploymentHis.getId();
		if(!StringUtil.isEmpty(lcDeploymentHis.getLcProcessId()) && !StringUtil.isEmpty(hid)){
			Map<String,Object> condition = new HashMap<>();
			condition.put("mountsId",lcDeploymentHis.getLcProcessId());
			List<LcNodeAttribute> lcNodeAttributes = lcNodeAttributeService.getLcNodeAttributeListByCondition(condition);
			if(!CollectionUtil.isEmpty(lcNodeAttributes)){
				for(LcNodeAttribute lcNodeAttribute: lcNodeAttributes){
					lcNodeAttribute = lcNodeAttributeService.getLcNodeAttributeById(lcNodeAttribute.getId());
					lcNodeAttribute.setMountsId(null);
					lcNodeAttribute.setId(null);
					lcNodeAttribute.setHid_(hid);
					lcNodeAttributeService.saveLcNodeAttribute(lcNodeAttribute);
				}
			}
		}
	}

	/**
	 * 处理节点
	 * @param lcProcess
	 */
	private void doAttr(LcProcess lcProcess){
		//如果节点已被删除则需要删除已配置的节点规则
		if(null != lcProcess){
			String bpmn = lcProcess.getBpmn();
			if(!StringUtil.isEmpty(bpmn)){
				List<UserTask> userTasks = activitiUtil.getUserTaskElementsByBpmn(bpmn);
				List<String> nodeIds = new ArrayList<>();
				if(!CollectionUtil.isEmpty(userTasks)){
					for(UserTask userTask: userTasks){
						nodeIds.add(userTask.getId());
					}
				}
				Map<String,Object> condition = new HashMap<>();
				if(!CollectionUtil.isEmpty(nodeIds)){
					condition.put("nodeId",StringUtils.join(nodeIds.toArray(),","));
				}
				condition.put("mountsId",lcProcess.getId());
				List<LcNodeAttribute> lcNodeAttributes = lcNodeAttributeService.getLcNodeAttributeListByCondition(condition);
				List<LcNodeAttribute> newLcNodeAttributes = new ArrayList<>();
				if(!CollectionUtil.isEmpty(lcNodeAttributes)){
					for(LcNodeAttribute lcNodeAttribute : lcNodeAttributes){
						lcNodeAttribute = lcNodeAttributeService.getLcNodeAttributeById(lcNodeAttribute.getId());
						newLcNodeAttributes.add(lcNodeAttribute);
					}
				}
				condition = new HashMap<>();
				condition.put("mountsId",lcProcess.getId());
				List<LcNodeAttribute> oldLcNodeAttributes = lcNodeAttributeService.getLcNodeAttributeListByCondition(condition);//原始数据

				if(!CollectionUtil.isEmpty(oldLcNodeAttributes)){
					//删除当前流程下的所有已配置节点规则
					for(LcNodeAttribute lcNodeAttribute : oldLcNodeAttributes){
						lcNodeAttributeService.delLcNodeAttributeById(lcNodeAttribute.getId());
					}
				}
				if(!CollectionUtil.isEmpty(newLcNodeAttributes)){//重新新增
					for(LcNodeAttribute lcNodeAttribute: newLcNodeAttributes){
						lcNodeAttribute.setId(null);
						lcNodeAttributeService.saveLcNodeAttribute(lcNodeAttribute);
					}
				}
			}
		}
	}

	/**
	 * 生成模块Key
	 * @return
	 */
	private synchronized String genModuleKey(){
		return ""+snowflakeIdWorker.nextId();
	}

	/**
	 * 导出流程
	 * @param id
	 */
	public void exportProcess(HttpServletResponse response,String id){
		try{
			if(StringUtil.isEmpty(id)){
				log.warn("未能获取到流程id");
				throw new ExceptionUtil("未能获取到流程id");
			}
			LcProcess lcProcess = lcProcessDao.getLcProcessById(id);
			if(null == lcProcess){
				log.warn("未能获取到流程信息");
				throw new ExceptionUtil("未能获取到流程信息");
			}
			//1.主流程配置类信息
			LcProcessEntity lcProcessEntity = new LcProcessEntity();
			lcProcessEntity.setLcProcess(lcProcess);
			//2.节点属性
			Map<String,Object> condition = new HashMap<>();
			condition.put("mountsId",id);
			List<LcNodeAttribute> lcNodeAttributes = lcNodeAttributeService.getLcNodeAttributeListByCondition(condition);
			List<LcNodeAttribute> lcNodeAttributeList = new ArrayList<>();
			if(!CollectionUtil.isEmpty(lcNodeAttributes)){
				for(LcNodeAttribute lcNodeAttribute: lcNodeAttributes){
					lcNodeAttribute = lcNodeAttributeService.getLcNodeAttributeById(lcNodeAttribute.getId());
					lcNodeAttribute.setMountsId(id);
					lcNodeAttribute.setHid_(null);
					lcNodeAttribute.setLcProcessId(id);
					lcNodeAttributeList.add(lcNodeAttribute);
				}
			}
			lcProcessEntity.setLcNodeAttributes(lcNodeAttributeList);
			String json = JsonUtil.toFastJson(lcProcessEntity);
			FileUtil.exportJsonFile(response,json,lcProcess.getTitle()+".json");
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导入流程
	 * @param request
	 * @param response
	 * @param multipartFile
	 * @return
	 */
	public BaseResult importProcess(HttpServletRequest request,HttpServletResponse response,MultipartFile multipartFile){
		String jsonText = FileUtil.readJson(multipartFile);
		if(StringUtil.isEmpty(jsonText)){
			throw new ExceptionUtil("导入内容为空！");
		}
		LcProcessEntity lcProcessEntity = JsonUtil.fromAliFastJson(jsonText,LcProcessEntity.class);
		if(null == lcProcessEntity || null == lcProcessEntity.getLcProcess()){
			throw new ExceptionUtil("未能解析到流程信息！");
		}
		if(StringUtil.isEmpty(lcProcessEntity.getLcProcess().getId())){
			throw new ExceptionUtil("未能解析到流程id！");
		}

		LcProcess processTemp = getLcProcessById(lcProcessEntity.getLcProcess().getId());
		if(null != processTemp){//解决可覆盖主模板逻辑
			if(!StringUtil.isEmpty(lcProcessEntity.getLcProcess().getModuleKey())){
				if(!processTemp.getModuleKey().equals(lcProcessEntity.getLcProcess().getModuleKey())){
					Map<String,Object> condition = new HashMap<>();
					condition.put("moduleKey",lcProcessEntity.getLcProcess().getModuleKey());
					List<LcProcess> list = lcProcessDao.getLcProcessListByCondition(condition);
					if(!CollectionUtil.isEmpty(list)){
						throw new ExceptionUtil("流程模块Key已存在，Key:"+lcProcessEntity.getLcProcess().getModuleKey());
					}
				}
			}
		}

		if(StringUtil.isEmpty(lcProcessEntity.getLcProcess().getModuleKey())){
			lcProcessEntity.getLcProcess().setModuleKey(genModuleKey());//重新生成模块key
		}

		LcProcess lcProcess = lcProcessEntity.getLcProcess();
		List<LcNodeAttribute> lcNodeAttributes = lcProcessEntity.getLcNodeAttributes();
		MxGraphModel mxGraphModel = new MxGraphModel();
		mxGraphModel.setBpmn(null);
		mxGraphModel.setCandidateGroupType(lcProcess.getCandidateGroupType());
		mxGraphModel.setImgXml(lcProcess.getImgXml());
		mxGraphModel.setMxgraphXml(lcProcess.getMxgraphXml());
		mxGraphModel.setW(lcProcess.getW());
		mxGraphModel.setH(lcProcess.getH());
		mxGraphModel.setId(lcProcess.getId());
		mxGraphModel.setCandidateStarterGroups(lcProcess.getCandidateStarterGroups());
		mxGraphModel.setCandidateStarterUsers(lcProcess.getCandidateStarterUsers());
		mxGraphModel.setRemark(lcProcess.getRemark());
		mxGraphModel.setModuleKey(lcProcess.getModuleKey());
		mxGraphModel.setLcProductId(lcProcess.getLcProductId());
		mxGraphModel.setLcGroupId(lcProcess.getLcGroupId());
		mxGraphModel.setTitle(lcProcess.getTitle());
		mxGraphModel.setFromImport(true);//标记为导入来源
		BaseResult<LcProcess> baseResult = create(mxGraphModel,request,response);
		if(!baseResult.getSuccess()){
			return BaseResult.fail("导入主数据异常");
		}

		//扩展属性
		lcNodeAttributeService.delLcNodeAttributeByMountsId(lcProcess.getId());//根据挂载编号（流程主模板id即最新版本）删除节点配置及其对应的所有关联配置
		if(!CollectionUtil.isEmpty(lcNodeAttributes)){
			for(LcNodeAttribute lcNodeAttribute: lcNodeAttributes){
				lcNodeAttributeService.syncLcNodeAttribute(lcNodeAttribute);
			}
		}

		return BaseResult.success();
	}
}
